package com.own.business.system.user.business.method;

import com.own.business.system.account.business.service.SystemAccountService;
import com.own.business.system.account.common.base.BaseAccountType;
import com.own.business.system.account.common.em.AccountType;
import com.own.business.system.account.common.hook.AccountHook;
import com.own.component.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * AccountUsernameMethodHook
 * <p>
 * 账号密码登录
 *
 * @author chenxueli
 * @date 2023-06-19 13:55:00
 */
@Slf4j
@Component
public class AccountUsernameMethodHook implements AccountHook {

    @Resource
    private SystemAccountService systemAccountService;

    /**
     * 添加账号信息
     *
     * @param account     账号
     * @param userId      用户id
     * @param accountType 账号类型 1=普通用户 2=管理员
     * @param password    密码（未加密）
     */
    @Override
    public void add(String account, Long userId, int accountType, String password) {
        if (StringUtils.isBlank(password)) {
            throw new BusinessException("密码不能为空");
        }
        systemAccountService.save(account, password, type(), accountType, userId);
    }

    /**
     * 账号的类型
     *
     * @return 账号的类型
     */
    @Override
    public BaseAccountType type() {
        return AccountType.TEXT;
    }

}
